package com.example.uit_management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class Activity_list_schedule extends Activity {

	private JsonParser_regCourse Json_regcourse=new JsonParser_regCourse();
	private JsonParser_course Json_course=new JsonParser_course();    	
	private ListView list;	
	private ArrayAdapterSchedule adapter;	
	private ArrayList<HashMap<String, String>> array_json_regcourse=new ArrayList<HashMap<String, String>>();
	private ArrayList<HashMap<String, String>> array_json_course=new ArrayList<HashMap<String, String>>();
	private HashMap<String, String> json_regcourse=new HashMap<String, String>();
	private HashMap<String, String> json_course=new HashMap<String, String>();
	private ArrayList<HashMap<String, String>> array_MAP=new ArrayList<HashMap<String, String>>();
	private String Json_string ;
	private String Day_of_week;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_schedule);
        Menu_Activity.dialog.dismiss();
        // Thong bao ngay trong tuan
        try{
        Day_of_week="";        
        // Lay thong tin cac mon hoc trong hoc ki nay
        array_json_course = Json_course.getMap_full();
        // Lay thong tin  cac mon hoc sinh vien da dang ki
        array_json_regcourse = Json_regcourse.getMap();
        Log.e(Activity_list_schedule.class.toString(), "Vong lap");
        // duyet tung mon da dang ki rui tim thong tin ve no
        for (int i = 0; i < array_json_regcourse.size(); i++) {  
        	json_regcourse = array_json_regcourse.get(i);        	

			for (int j = 0; j < array_json_course.size(); j++) {
				json_course = array_json_course.get(j);
	        	if(json_regcourse.get(JsonParser_regCourse.KEY_courseID).equals(json_course.get(JsonParser_course.KEY_courseID))){
					array_MAP.add(json_course); 
					if(json_course.get(JsonParser_course.KEY_courseDay).equals(Integer.toString(MainActivity.day))){
						 Day_of_week +="-Môn : "+ json_course.get(JsonParser_course.KEY_subjectName)+"\n\tphòng :"+json_course.get(JsonParser_course.KEY_roomID)+"-Ca : "+json_course.get(JsonParser_course.KEY_courseTime)+ "\n";
		        	} 
					break;
				}
			}
		}
        // Show thong bao cac mon hoc hom nay
        Show_Alert(Day_of_week);
        list=(ListView)findViewById(R.id.list_schedule);
        // call constructor method of adapter       
        Collections.sort(array_MAP, new ArrayMap());        
        adapter =new ArrayAdapterSchedule(this, array_MAP);
        // add adapter into list
        if(array_MAP.size()==0){
        	Toast.makeText(this, "Không có môn nào", Toast.LENGTH_LONG).show();
        }
        list.setAdapter(adapter);     
        }catch(Exception e){
        	e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity__score, menu);
        return true;
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_back:{
        	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
				startActivity(intent);				
				break;
        	}
        }
        return true;
    }
    /**
     *Ham thong bao voi noi dung Day_of_week
     * */
    public void Show_Alert(String Day_of_week){
   	 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(Activity_list_schedule.this);
   	 myAlertDialog.setTitle("MÔN HỌC THỨ "+MainActivity.day);
   	 if(Day_of_week=="")  	 {
   		 myAlertDialog.setMessage("HÔM NAY KHÔNG CÓ HỌC");
   	 }else
   		 myAlertDialog.setMessage(Day_of_week);

   	 myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

   	  public void onClick(DialogInterface arg0, int arg1) {
   	  // do something when the OK button is clicked
   	  }});   	
   	 myAlertDialog.show();
   }
}

